package com.gzyj.jtt808.utils;

/**
 * @author lhj
 */
public class IntUtils {
    /**
     * 将一个int数字转换为二进制的字符串形式。
     *
     * @param num 需要转换的int类型数据
     * @return 二进制的字符串形式
     */
    public static String intToBinary(int num, int bitNum) {
        //1.补零
        String binaryStr = Integer.toBinaryString(num);

        if (bitNum < binaryStr.length()) {
            bitNum += bitNum;//不断翻倍8 16 32 64...
        }
        while (binaryStr.length() < bitNum) {
            binaryStr = "0" + binaryStr;
        }
        return binaryStr;
    }

    public static String longToBinary(long num, int bitNum) {
        //1.补零
        String binaryStr = Long.toBinaryString(num);

        if (bitNum < binaryStr.length()) {
            bitNum += bitNum;//不断翻倍8 16 32 64...
        }
        while (binaryStr.length() < bitNum) {
            binaryStr = "0" + binaryStr;
        }
        return binaryStr;
    }
}
